package com.blog.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.pojo.Blog;
import com.blog.pojo.Comment;
import com.blog.service.BlogService;
import com.blog.service.CommentService;
import com.blog.util.ErrorMsg;
/**
 * 添加评论
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

	@Resource
	private CommentService commentService;
	@Resource
	private BlogService blogService;
	
	/**
	 * 添加或修改评论
	 * comment 评论参数
	 * imageCode 验证码
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	private Model add(Comment comment,/*String imageCode,*/Model model,HttpServletRequest request,HttpSession httpSession){
		//获取系统生成的评论验证码
		String sRend = (String) httpSession.getAttribute("sRend");
		//判断验证码是否正确
	/*	if(!imageCode.equals(sRend)){
			ErrorMsg.successFalse(model, ErrorMsg.iamgeCode);
			return model;
		}else{*/
			//获取用户ip
			String userIp = request.getRemoteAddr();
			comment.setUserIp(userIp);
			int isok = commentService.add(comment);
			if(isok > 0){
				// 成功后该博客的回复次数加1
				Blog blog = blogService.findById(comment.getBlog().getId());
				blog.setReplyHit(blog.getReplyHit()+1);
				blogService.update(blog);
				model.addAttribute("success", true);
			}else{
				ErrorMsg.successFalse(model, ErrorMsg.successFalse);
			}
	//	}
		return model;
	}
}
